import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:ota_update/ota_update.dart';
import './routers/routers.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarColor: Colors.transparent,statusBarIconBrightness: Brightness.light));
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  Future<void> checkForUpdate() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String currentVersion = packageInfo.version;

    // 假设服务器上的最新版本是 "2.0.0"
    String serverVersion = "2.0.0";

    print(currentVersion);
    if (currentVersion != serverVersion) {
      // 版本不匹配，提示用户进行更新
      // 可以使用如firebase、应用内通知等方式通知用户
      gengxin();
      print('发现新版本，请更新！');
    } else {
      print('已是最新版本');
    }
  }


  void gengxin(){

    String fileName = "app-debug.apk";//包名称必须保持一致
    String url = "http://192.168.2.126/app-debug.apk";//线上更新包地址
    String _progress = '0';

    OtaUpdate()
        .execute(url, destinationFilename: fileName)
        .listen(
          (OtaEvent event) async{
        switch (event.status) {
          case OtaStatus.DOWNLOADING: // 下载中
            _progress = event.value!;
            print('下载进度：${event.value!}%');
            break;
          case OtaStatus.INSTALLING: //安装中
            _progress = '100';
            break;
          case OtaStatus.PERMISSION_NOT_GRANTED_ERROR: // 权限错误
            print('由于缺少权限，无法继续！');
            break;
          default: // 其他问题
            break;
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    //checkForUpdate();


    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      initialRoute: '/',
      onGenerateRoute: onGenerateRoute,

    );
  }
}
